package br.net.woodstock.tickettoride.web.jsf.manager.common;

import java.util.ArrayList;
import java.util.Collection;

import javax.faces.model.SelectItem;

import br.net.woodstock.tickettoride.api.access.UserService;
import br.net.woodstock.tickettoride.api.manager.ApplicationService;
import br.net.woodstock.tickettoride.orm.Application;
import br.net.woodstock.tickettoride.orm.User;
import br.net.woodstock.tickettoride.orm.UserType;
import br.net.woodstock.tickettoride.web.jsf.TTRBean;
import br.net.woodstock.tickettoride.web.jsf.security.common.JSFSecurityUtils;

public abstract class AbstractApplicationBean extends TTRBean {

	private static final long		serialVersionUID	= -7676757968836904361L;

	private static final String		MAIN_VIEW			= "/manager/application.xhtml";

	private static final String		LIST_VIEW			= "/manager/application-list.xhtml";

	private static final String		SAVE_VIEW			= "/manager/application-save.xhtml";

	private static final String		PERMISSION_VIEW		= "/manager/application-permission.xhtml";

	private static final String		MANAGER_VIEW		= "/manager/application-manager.xhtml";

	private Integer					id;

	private String					alias;

	private String					name;

	private String					description;

	private String					path;

	private String					view;

	private String					filter;

	private boolean					listing;

	private Application				application;

	private Collection<Application>	applications;

	private Collection<SelectItem>	sourceApplications;

	private Collection<SelectItem>	sourceUsers;

	private Object[]				permitedApplications;

	private Object[]				permitedUsers;

	public AbstractApplicationBean() {
		super();
		this.view = AbstractApplicationBean.LIST_VIEW;
	}

	public abstract ApplicationService getApplicationService();

	public abstract UserService getUserService();

	public String start() {
		this.onStart();
		return AbstractApplicationBean.MAIN_VIEW;
	}

	public String end() {
		this.onEnd();
		return TTRBean.INDEX_VIEW;
	}

	public String newApplication() {
		this.id = null;
		this.alias = null;
		this.name = null;
		this.description = null;
		this.path = null;
		this.view = AbstractApplicationBean.SAVE_VIEW;
		return null;
	}

	public String list() {
		if (JSFSecurityUtils.isUserInRole(UserType.ADMINISTRATOR.name())) {
			this.applications = this.getApplicationService().listByFilter(this.filter);
			this.listing = true;
		} else {
			this.applications = this.getApplicationService().listByManager(JSFSecurityUtils.getSessionUser().getId(), this.filter);
			this.listing = true;
		}
		return null;
	}

	public String edit() {
		Application application = this.getApplicationService().getById(this.id);
		this.id = application.getId();
		this.alias = application.getAlias();
		this.name = application.getName();
		this.description = application.getDescription();
		this.path = application.getPath();
		this.view = AbstractApplicationBean.SAVE_VIEW;
		return null;
	}

	public String save() {
		Application application = new Application();
		application.setAlias(this.alias);
		application.setName(this.name);
		application.setDescription(this.description);
		application.setPath(this.path);

		if (this.isValidId(this.id)) {
			application.setId(this.id);
			this.getApplicationService().update(application);
		} else {
			this.getApplicationService().save(application);
			this.id = null;
			this.alias = null;
			this.name = null;
			this.description = null;
			this.path = null;
		}

		this.addMessage(this.getMessageOperationOK());

		return null;
	}

	public String permission() {
		this.application = this.getApplicationService().getById(this.id);
		Collection<Application> src = this.getApplicationService().listByFilter(null);
		this.sourceApplications = new ArrayList<SelectItem>();
		for (Application a : src) {
			this.sourceApplications.add(new SelectItem(a.getId(), a.getAlias() + " - " + a.getName()));
		}

		Collection<Application> dst = this.getApplicationService().getPermiteds(this.id);
		this.permitedApplications = new Integer[dst.size()];
		int index = 0;
		for (Application a : dst) {
			this.permitedApplications[index++] = a.getId();
		}

		this.view = AbstractApplicationBean.PERMISSION_VIEW;
		return null;
	}

	public String manager() {
		this.application = this.getApplicationService().getById(this.id);
		Collection<User> src = this.getUserService().listByFilter(null);
		this.sourceUsers = new ArrayList<SelectItem>();
		for (User u : src) {
			this.sourceUsers.add(new SelectItem(u.getId(), u.getName()));
		}

		Collection<User> dst = this.getApplicationService().getManagers(this.id);
		this.permitedUsers = new Integer[dst.size()];
		int index = 0;
		for (User u : dst) {
			this.permitedUsers[index++] = u.getId();
		}

		this.view = AbstractApplicationBean.MANAGER_VIEW;
		return null;
	}

	public String savePermission() {
		Integer[] array = new Integer[this.permitedApplications.length];
		for (int i = 0; i < this.permitedApplications.length; i++) {
			array[i] = Integer.valueOf(this.permitedApplications[i].toString());
		}
		this.getApplicationService().saveApplicationPermission(this.id, array);
		this.permission();
		this.addMessage(this.getMessageOperationOK());
		return null;
	}

	public String saveManager() {
		Integer[] array = new Integer[this.permitedUsers.length];
		for (int i = 0; i < this.permitedUsers.length; i++) {
			array[i] = Integer.valueOf(this.permitedUsers[i].toString());
		}
		this.getApplicationService().saveApplicationManager(this.id, array);
		this.manager();
		this.addMessage(this.getMessageOperationOK());
		return null;
	}

	public String cancel() {
		this.view = AbstractApplicationBean.LIST_VIEW;
		if (this.applications != null) {
			this.list();
		}
		this.sourceApplications = null;
		this.permitedApplications = null;
		return null;
	}

	// Getters and Setters
	public Integer getId() {
		return this.id;
	}

	public void setId(final Integer id) {
		this.id = id;
	}

	public String getAlias() {
		return this.alias;
	}

	public void setAlias(final String alias) {
		this.alias = alias;
	}

	public String getName() {
		return this.name;
	}

	public void setName(final String name) {
		this.name = name;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(final String description) {
		this.description = description;
	}

	public String getPath() {
		return this.path;
	}

	public void setPath(final String path) {
		this.path = path;
	}

	public void setView(final String view) {
		this.view = view;
	}

	public String getView() {
		return this.view;
	}

	public String getFilter() {
		return this.filter;
	}

	public void setFilter(final String filter) {
		this.filter = filter;
	}

	public boolean isListing() {
		return this.listing;
	}

	public void setListing(final boolean listing) {
		this.listing = listing;
	}

	public Application getApplication() {
		return this.application;
	}

	public void setApplication(final Application application) {
		this.application = application;
	}

	public Collection<Application> getApplications() {
		return this.applications;
	}

	public void setApplications(final Collection<Application> applications) {
		this.applications = applications;
	}

	public Collection<SelectItem> getSourceApplications() {
		return this.sourceApplications;
	}

	public void setSourceApplications(final Collection<SelectItem> sourceApplications) {
		this.sourceApplications = sourceApplications;
	}

	public Object[] getPermitedApplications() {
		return this.permitedApplications;
	}

	public void setPermitedApplications(final Object[] permitedApplications) {
		this.permitedApplications = permitedApplications;
	}

	public Collection<SelectItem> getSourceUsers() {
		return this.sourceUsers;
	}

	public void setSourceUsers(final Collection<SelectItem> sourceUsers) {
		this.sourceUsers = sourceUsers;
	}

	public Object[] getPermitedUsers() {
		return this.permitedUsers;
	}

	public void setPermitedUsers(final Object[] permitedUsers) {
		this.permitedUsers = permitedUsers;
	}

}
